package com.gtw.modules.quartz;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.utils.DateUtils;
import com.gtw.common.utils.PayStatus;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.WeiXinMsgUtil;
import com.gtw.modules.api.service.*;
import com.gtw.modules.dao.TbActivityCommodityDao;
import com.gtw.modules.entity.*;
import com.gtw.modules.sys.service.*;
import com.gtw.utils.DateUtil;
import com.gtw.utils.JSONUtils;
import com.gtw.utils.OrderUtil;
import com.gtw.utils.UidUtil;
import net.sf.json.JSONArray;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 定时器
 *
 * @author wuye
 * @date 2018/1/25
 */
@Component
@Configurable
@EnableScheduling
@EnableAsync
public class QuartzTask {


    @Autowired
    private TbDividendsService tbDividendsService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbGroupbuyService tbGroupbuyService;

    @Autowired
    private TbActivityServices tbActivityServices;

    @Autowired
    private TbCapitalpoolService tbCapitalpoolService;

    @Autowired
    private TbCharmvalueNotesService tbCharmvalueNotesService;

    @Autowired
    private TbEnergyvalueNotesService tbEnergyvalueNotesService;

    @Autowired
    private TbProxyDividendDetailsService tbProxyDividendDetailsService;

    @Autowired
    private TbRaidDividendDetailsService tbRaidDividendDetailsService;

    @Autowired
    private TbGroupbuyUserService tbGroupbuyUserService;

    @Autowired
    private TbOrderService tbOrderService;

    @Autowired
    private SysDataInfoService sysDataInfoService;

    @Autowired
    private TbBargainService tbBargainService;

    @Autowired
    private TbCommodityService tbCommodityService;

    @Autowired
    private TbShoppingCartServices tbShoppingCartServices;

    @Autowired
    private TbAddressService tbAddressService;
    @Autowired
    private TbActivityCommodityDao tbActivityCommodityDao;

    public QuartzTask() {
        System.out.println("定时任务初始化");
    }

    @Scheduled(fixedRate = 5000)//每30秒执行一次
    public void aaa() throws Exception {
        System.out.println("测试数据：" + new Date());
        TbUser user = tbUserService.queryByUid("51d5d7f3f63c48c0b2dce6ee02c5700f");
        TbUser user2 = tbUserService.queryByUid("f8d19c27fd4047b4bbef58d9c8551994");
        System.out.println("测试数据User：" + user.getUsername() + " " + new Date());
        System.out.println("测试数据User2：" + user2.getUsername() + " " + new Date());
    }

    /**
     * 定时任务 会员积分等级换算
     */
    @Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行
    public void work0() throws Exception {
        //获取所有用户信息
        List<TbUser> userList = tbUserService.queryAll();
        for (TbUser tbuser : userList) {

            /**
             * 魅力值等级换算
             */
            TbDividends tbDividends1 = tbDividendsService.queryByLevel(1);
            if (tbuser.getCharmvalue() >= tbDividends1.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends1.getIntegralMax()) {
                tbuser.setIntegrallevel(1);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends2 = tbDividendsService.queryByLevel(2);
            if (tbuser.getCharmvalue() >= tbDividends2.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends2.getIntegralMax()) {
                tbuser.setIntegrallevel(2);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends3 = tbDividendsService.queryByLevel(3);
            if (tbuser.getCharmvalue() >= tbDividends3.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends3.getIntegralMax()) {
                tbuser.setIntegrallevel(3);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends4 = tbDividendsService.queryByLevel(4);
            if (tbuser.getCharmvalue() >= tbDividends4.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends4.getIntegralMax()) {
                tbuser.setIntegrallevel(4);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends5 = tbDividendsService.queryByLevel(5);
            if (tbuser.getCharmvalue() >= tbDividends5.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends5.getIntegralMax()) {
                tbuser.setIntegrallevel(5);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends6 = tbDividendsService.queryByLevel(6);
            if (tbuser.getCharmvalue() >= tbDividends6.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends6.getIntegralMax()) {
                tbuser.setIntegrallevel(6);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividends7 = tbDividendsService.queryByLevel(7);
            if (tbuser.getCharmvalue() >= tbDividends7.getIntegralMin() && tbuser.getCharmvalue() <= tbDividends7.getIntegralMax()) {
                tbuser.setIntegrallevel(7);
                tbUserService.update(tbuser);
            }

            /**
             * 能量值积分换算
             */
            TbDividends tbDividendsl11 = tbDividendsService.queryByLevel2(1);
            if (tbuser.getEnergyvalue() >= tbDividendsl11.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl11.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(1);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividendsl22 = tbDividendsService.queryByLevel2(2);
            if (tbuser.getEnergyvalue() >= tbDividendsl22.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl22.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(2);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividendsl33 = tbDividendsService.queryByLevel2(3);
            if (tbuser.getEnergyvalue() >= tbDividendsl33.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl33.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(3);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividendsl44 = tbDividendsService.queryByLevel2(4);
            if (tbuser.getEnergyvalue() >= tbDividendsl44.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl44.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(4);
                tbUserService.update(tbuser);
            }
            TbDividends tbDividendsl55 = tbDividendsService.queryByLevel2(5);
            if (tbuser.getEnergyvalue() >= tbDividendsl55.getIntegralMin() && tbuser.getEnergyvalue() <= tbDividendsl55.getIntegralMax()) {
                tbuser.setEnergyvaluelevel(5);
                tbUserService.update(tbuser);
            }
        }
    }


    /**
     * 定时任务 会员积分日耗（每日凌晨执行）
     *
     * @throws Exception
     */
    @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行
    public void work() throws Exception {
        ReentrantLock lock1 = new ReentrantLock();
        try {
            lock1.lock();
            /*
             * 会员魅力值积分日耗结算
             */
            //获取魅力值等级为1的用户
            List<TbUser> tbUsers1 = tbUserService.queryByintegrallevel1(1);
            //获取魅力值等级为1的积分日耗数据
            TbDividends tbDividends1 = tbDividendsService.queryByLevel(1);
            for (TbUser user1 : tbUsers1) {
                if (user1.getCharmvalue() >= 1) {
                    //小数处理(有值就递进一位)
                    Integer newvalue1 = (int) Math.ceil(user1.getCharmvalue() * tbDividends1.getIntegralConsumption());
                    if ((user1.getCharmvalue() - newvalue1) >= 0) {
                        user1.setCharmvalue(user1.getCharmvalue() - newvalue1);
                        if (user1.getCharmvalue() > tbDividends1.getIntegralMax()) {
                            user1.setIntegrallevel(user1.getIntegrallevel() + 1);
                        } else {
                            if (user1.getIntegrallevel() > 0) {
                                user1.setIntegrallevel(user1.getIntegrallevel() - 1);
                            }
                        }
                        tbUserService.update(user1);
                        //会员积分消耗明细
                        TbCharmvalueNotes tbCharmvalueNotes = new TbCharmvalueNotes();
                        tbCharmvalueNotes.setCreattime(new Date());
                        tbCharmvalueNotes.setType(1);
                        tbCharmvalueNotes.setOrderid(0);
                        tbCharmvalueNotes.setUid(user1.getUuid());
                        tbCharmvalueNotes.setValue((newvalue1));
                        tbCharmvalueNotesService.save(tbCharmvalueNotes);
                    }
                }
            }

            //获取魅力值等级为2的用户
            List<TbUser> tbUsers2 = tbUserService.queryByintegrallevel1(2);
            //获取魅力值等级为2的积分日耗数据
            TbDividends tbDividends2 = tbDividendsService.queryByLevel(2);
            for (TbUser user2 : tbUsers2) {
                if (user2.getCharmvalue() >= 1) {
                    //小数处理(有值就递进一位)
                    Integer newvalue2 = (int) Math.ceil(user2.getCharmvalue() * tbDividends2.getIntegralConsumption());
                    if ((user2.getCharmvalue() - newvalue2) >= 0) {
                        user2.setCharmvalue(user2.getCharmvalue() - newvalue2);
                        if (user2.getCharmvalue() > tbDividends2.getIntegralMax()) {
                            user2.setIntegrallevel(user2.getIntegrallevel() + 1);
                        } else {
                            user2.setIntegrallevel(user2.getIntegrallevel() - 1);
                        }
                        tbUserService.update(user2);
                        //会员积分消耗明细
                        TbCharmvalueNotes tbCharmvalueNotes = new TbCharmvalueNotes();
                        tbCharmvalueNotes.setCreattime(new Date());
                        tbCharmvalueNotes.setType(1);
                        tbCharmvalueNotes.setOrderid(0);
                        tbCharmvalueNotes.setUid(user2.getUuid());
                        tbCharmvalueNotes.setValue((newvalue2));
                        tbCharmvalueNotesService.save(tbCharmvalueNotes);
                    }
                }
            }

            //获取魅力值等级为3的用户
            List<TbUser> tbUsers3 = tbUserService.queryByintegrallevel1(3);
            //获取魅力值等级为3的积分日耗数据
            TbDividends tbDividends3 = tbDividendsService.queryByLevel(3);
            for (TbUser user3 : tbUsers3) {
                if (user3.getCharmvalue() >= 1) {
                    //小数处理(有值就递进一位)
                    Integer newvalue3 = (int) Math.ceil(user3.getCharmvalue() * tbDividends3.getIntegralConsumption());
                    if ((user3.getCharmvalue() - newvalue3) >= 0) {
                        user3.setCharmvalue(user3.getCharmvalue() - newvalue3);
                        if (user3.getCharmvalue() > tbDividends3.getIntegralMax()) {
                            user3.setIntegrallevel(user3.getIntegrallevel() + 1);
                        } else {
                            user3.setIntegrallevel(user3.getIntegrallevel() - 1);
                        }
                        tbUserService.update(user3);
                        //会员积分消耗明细
                        TbCharmvalueNotes tbCharmvalueNotes = new TbCharmvalueNotes();
                        tbCharmvalueNotes.setCreattime(new Date());
                        tbCharmvalueNotes.setType(1);
                        tbCharmvalueNotes.setOrderid(0);
                        tbCharmvalueNotes.setUid(user3.getUuid());
                        tbCharmvalueNotes.setValue((newvalue3));
                        tbCharmvalueNotesService.save(tbCharmvalueNotes);
                    }
                }
            }

            //获取魅力值等级为4的用户
            List<TbUser> tbUsers4 = tbUserService.queryByintegrallevel1(4);
            //获取魅力值等级为4的积分日耗数据
            TbDividends tbDividends4 = tbDividendsService.queryByLevel(4);
            for (TbUser user4 : tbUsers4) {
                if (user4.getCharmvalue() >= 1) {
                    //小数处理(有值就递进一位)
                    Integer newvalue4 = (int) Math.ceil(user4.getCharmvalue() * tbDividends4.getIntegralConsumption());
                    if ((user4.getCharmvalue() - newvalue4) >= 0) {
                        user4.setCharmvalue(user4.getCharmvalue() - newvalue4);
                        if (user4.getCharmvalue() > tbDividends4.getIntegralMax()) {
                            user4.setIntegrallevel(user4.getIntegrallevel() + 1);
                        } else {
                            user4.setIntegrallevel(user4.getIntegrallevel() - 1);
                        }
                        tbUserService.update(user4);
                        //会员积分消耗明细
                        TbCharmvalueNotes tbCharmvalueNotes = new TbCharmvalueNotes();
                        tbCharmvalueNotes.setCreattime(new Date());
                        tbCharmvalueNotes.setType(1);
                        tbCharmvalueNotes.setOrderid(0);
                        tbCharmvalueNotes.setUid(user4.getUuid());
                        tbCharmvalueNotes.setValue((newvalue4));
                        tbCharmvalueNotesService.save(tbCharmvalueNotes);
                    }
                }
            }

            //获取魅力值等级为5的用户
            List<TbUser> tbUsers5 = tbUserService.queryByintegrallevel1(5);
            //获取魅力值等级为5的积分日耗数据
            TbDividends tbDividends5 = tbDividendsService.queryByLevel(5);
            for (TbUser user5 : tbUsers5) {
                if (user5.getCharmvalue() >= 1) {
                    //小数处理(有值就递进一位)
                    Integer newvalue5 = (int) Math.ceil(user5.getCharmvalue() * tbDividends5.getIntegralConsumption());
                    if ((user5.getCharmvalue() - newvalue5) >= 0) {
                        user5.setCharmvalue(user5.getCharmvalue() - newvalue5);
                        if (user5.getCharmvalue() > tbDividends5.getIntegralMax()) {
                            user5.setIntegrallevel(user5.getIntegrallevel() + 1);
                        } else {
                            user5.setIntegrallevel(user5.getIntegrallevel() - 1);
                        }
                        tbUserService.update(user5);
                        //会员积分消耗明细
                        TbCharmvalueNotes tbCharmvalueNotes = new TbCharmvalueNotes();
                        tbCharmvalueNotes.setCreattime(new Date());
                        tbCharmvalueNotes.setType(1);
                        tbCharmvalueNotes.setOrderid(0);
                        tbCharmvalueNotes.setUid(user5.getUuid());
                        tbCharmvalueNotes.setValue((newvalue5));
                        tbCharmvalueNotesService.save(tbCharmvalueNotes);
                    }
                }
            }

            //获取魅力值等级为6的用户
            List<TbUser> tbUsers6 = tbUserService.queryByintegrallevel1(6);
            //获取魅力值等级为6的积分日耗数据
            TbDividends tbDividends6 = tbDividendsService.queryByLevel(6);
            for (TbUser user6 : tbUsers6) {
                if (user6.getCharmvalue() >= 1) {
                    //小数处理(有值就递进一位)
                    Integer newvalue6 = (int) Math.ceil(user6.getCharmvalue() * tbDividends6.getIntegralConsumption());
                    if ((user6.getCharmvalue() - newvalue6) >= 0) {
                        user6.setCharmvalue(user6.getCharmvalue() - newvalue6);
                        if (user6.getCharmvalue() > tbDividends6.getIntegralMax()) {
                            user6.setIntegrallevel(user6.getIntegrallevel() + 1);
                        } else {
                            user6.setIntegrallevel(user6.getIntegrallevel() - 1);
                        }
                        tbUserService.update(user6);
                        //会员积分消耗明细
                        TbCharmvalueNotes tbCharmvalueNotes = new TbCharmvalueNotes();
                        tbCharmvalueNotes.setCreattime(new Date());
                        tbCharmvalueNotes.setType(1);
                        tbCharmvalueNotes.setOrderid(0);
                        tbCharmvalueNotes.setUid(user6.getUuid());
                        tbCharmvalueNotes.setValue((newvalue6));
                        tbCharmvalueNotesService.save(tbCharmvalueNotes);
                    }
                }
            }

            //获取魅力值等级为7的用户
            List<TbUser> tbUsers7 = tbUserService.queryByintegrallevel1(7);
            //获取魅力值等级为7的积分日耗数据
            TbDividends tbDividends7 = tbDividendsService.queryByLevel(7);
            for (TbUser user7 : tbUsers7) {
                if (user7.getCharmvalue() >= 1) {
                    //小数处理(有值就递进一位)
                    Integer newvalue7 = (int) Math.ceil(user7.getCharmvalue() * tbDividends7.getIntegralConsumption());
                    if ((user7.getCharmvalue() - newvalue7) >= 0) {
                        user7.setCharmvalue(user7.getCharmvalue() - newvalue7);
                        if (user7.getCharmvalue() > tbDividends7.getIntegralMax()) {
                            if (user7.getIntegrallevel() < 7) {
                                user7.setIntegrallevel(user7.getIntegrallevel() + 1);
                            }
                        } else {
                            user7.setIntegrallevel(user7.getIntegrallevel() - 1);
                        }
                        tbUserService.update(user7);
                        //会员积分消耗明细
                        TbCharmvalueNotes tbCharmvalueNotes = new TbCharmvalueNotes();
                        tbCharmvalueNotes.setCreattime(new Date());
                        tbCharmvalueNotes.setType(1);
                        tbCharmvalueNotes.setOrderid(0);
                        tbCharmvalueNotes.setUid(user7.getUuid());
                        tbCharmvalueNotes.setValue((newvalue7));
                        tbCharmvalueNotesService.save(tbCharmvalueNotes);
                    }
                }
            }

            /*
             * 会员能力值积分日耗结算
             */
            //获取能力值等级为1的用户
            List<TbUser> tbUserList1 = tbUserService.queryenergyvaluelevel(1);
            //获取能力值等级为1的积分日耗数据
            TbDividends tbDividendsl1 = tbDividendsService.queryByLevel2(1);
            for (TbUser user1 : tbUserList1) {
                if (user1.getType() == 2) {
                    if (user1.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue1 = (int) Math.ceil(user1.getEnergyvalue() * tbDividendsl1.getIntegralConsumption());
                        if ((user1.getEnergyvalue() - newvalue1) >= 0) {
                            user1.setEnergyvalue(user1.getEnergyvalue() - newvalue1);
                            if (user1.getCharmvalue() > tbDividendsl1.getIntegralMax()) {
                                user1.setEnergyvaluelevel(user1.getEnergyvaluelevel() + 1);
                            } else {
                                if (user1.getEnergyvaluelevel() > 0) {
                                    user1.setEnergyvaluelevel(user1.getEnergyvaluelevel() - 1);
                                }

                            }
                            tbUserService.update(user1);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue1);
                            tbEnergyvalueNotes.setUid(user1.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                } else if (user1.getType() == 1) {
                    if (user1.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue1 = (int) Math.ceil(user1.getEnergyvalue() * tbDividendsl1.getUserintegralconsumption());
                        if ((user1.getEnergyvalue() - newvalue1) >= 0) {
                            user1.setEnergyvalue(user1.getEnergyvalue() - newvalue1);
                            if (user1.getCharmvalue() > tbDividendsl1.getIntegralMax()) {
                                user1.setEnergyvaluelevel(user1.getEnergyvaluelevel() + 1);
                            } else {
                                if (user1.getEnergyvaluelevel() > 0) {
                                    user1.setEnergyvaluelevel(user1.getEnergyvaluelevel() - 1);
                                }

                            }
                            tbUserService.update(user1);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue1);
                            tbEnergyvalueNotes.setUid(user1.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                }
            }


            //获取能力值等级为2的用户
            List<TbUser> tbUserList2 = tbUserService.queryenergyvaluelevel(2);
            //获取能力值等级为2的积分日耗数据
            TbDividends tbDividendsl2 = tbDividendsService.queryByLevel2(2);
            for (TbUser user2 : tbUserList2) {
                if (user2.getType() == 2) {
                    if (user2.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue2 = (int) Math.ceil(user2.getEnergyvalue() * tbDividendsl2.getIntegralConsumption());
                        if ((user2.getEnergyvalue() - newvalue2) >= 0) {
                            user2.setEnergyvalue(user2.getEnergyvalue() - newvalue2);
                            if (user2.getCharmvalue() > tbDividendsl2.getIntegralMax()) {
                                user2.setEnergyvaluelevel(user2.getEnergyvaluelevel() + 1);
                            } else {
                                user2.setEnergyvaluelevel(user2.getEnergyvaluelevel() - 1);
                            }
                            tbUserService.update(user2);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue2);
                            tbEnergyvalueNotes.setUid(user2.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                } else if (user2.getType() == 1) {
                    if (user2.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue2 = (int) Math.ceil(user2.getEnergyvalue() * tbDividendsl2.getUserintegralconsumption());
                        if ((user2.getEnergyvalue() - newvalue2) >= 0) {
                            user2.setEnergyvalue(user2.getEnergyvalue() - newvalue2);
                            if (user2.getCharmvalue() > tbDividendsl2.getIntegralMax()) {
                                user2.setEnergyvaluelevel(user2.getEnergyvaluelevel() + 1);
                            } else {
                                user2.setEnergyvaluelevel(user2.getEnergyvaluelevel() - 1);
                            }
                            tbUserService.update(user2);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue2);
                            tbEnergyvalueNotes.setUid(user2.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                }
            }

            //获取能力值等级为3的用户
            List<TbUser> tbUserList3 = tbUserService.queryenergyvaluelevel(3);
            //获取能力值等级为3的积分日耗数据
            TbDividends tbDividendsl3 = tbDividendsService.queryByLevel2(3);
            for (TbUser user3 : tbUserList3) {
                if (user3.getType() == 2) {
                    if (user3.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue3 = (int) Math.ceil(user3.getEnergyvalue() * tbDividendsl3.getIntegralConsumption());
                        if ((user3.getEnergyvalue() - newvalue3) >= 0) {
                            user3.setEnergyvalue(user3.getEnergyvalue() - newvalue3);
                            if (user3.getCharmvalue() > tbDividendsl3.getIntegralMax()) {
                                user3.setEnergyvaluelevel(user3.getEnergyvaluelevel() + 1);
                            } else {
                                user3.setEnergyvaluelevel(user3.getEnergyvaluelevel() - 1);
                            }
                            tbUserService.update(user3);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue3);
                            tbEnergyvalueNotes.setUid(user3.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                } else if (user3.getType() == 1) {
                    if (user3.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue3 = (int) Math.ceil(user3.getEnergyvalue() * tbDividendsl3.getUserintegralconsumption());
                        if ((user3.getEnergyvalue() - newvalue3) >= 0) {
                            user3.setEnergyvalue(user3.getEnergyvalue() - newvalue3);
                            if (user3.getCharmvalue() > tbDividendsl3.getIntegralMax()) {
                                user3.setEnergyvaluelevel(user3.getEnergyvaluelevel() + 1);
                            } else {
                                user3.setEnergyvaluelevel(user3.getEnergyvaluelevel() - 1);
                            }
                            tbUserService.update(user3);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue3);
                            tbEnergyvalueNotes.setUid(user3.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                }
            }

            //获取能力值等级为4的用户
            List<TbUser> tbUserList4 = tbUserService.queryenergyvaluelevel(4);
            //获取能力值等级为4的积分日耗数据
            TbDividends tbDividendsl4 = tbDividendsService.queryByLevel2(4);
            for (TbUser user4 : tbUserList4) {
                if (user4.getType() == 2) {
                    if (user4.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue4 = (int) Math.ceil(user4.getEnergyvalue() * tbDividendsl4.getIntegralConsumption());
                        if ((user4.getEnergyvalue() - newvalue4) >= 0) {
                            user4.setEnergyvalue(user4.getEnergyvalue() - newvalue4);
                            if (user4.getCharmvalue() > tbDividendsl4.getIntegralMax()) {
                                user4.setEnergyvaluelevel(user4.getEnergyvaluelevel() + 1);
                            } else {
                                user4.setEnergyvaluelevel(user4.getEnergyvaluelevel() - 1);
                            }
                            tbUserService.update(user4);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue4);
                            tbEnergyvalueNotes.setUid(user4.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                } else if (user4.getType() == 1) {
                    if (user4.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue4 = (int) Math.ceil(user4.getEnergyvalue() * tbDividendsl4.getUserintegralconsumption());
                        if ((user4.getEnergyvalue() - newvalue4) >= 0) {
                            user4.setEnergyvalue(user4.getEnergyvalue() - newvalue4);
                            if (user4.getCharmvalue() > tbDividendsl4.getIntegralMax()) {
                                user4.setEnergyvaluelevel(user4.getEnergyvaluelevel() + 1);
                            } else {
                                user4.setEnergyvaluelevel(user4.getEnergyvaluelevel() - 1);
                            }
                            tbUserService.update(user4);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue4);
                            tbEnergyvalueNotes.setUid(user4.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                }
            }


            //获取能力值等级为5的用户
            List<TbUser> tbUserList5 = tbUserService.queryenergyvaluelevel(5);
            //获取能力值等级为5的积分日耗数据
            TbDividends tbDividendsl5 = tbDividendsService.queryByLevel2(5);
            for (TbUser user5 : tbUserList5) {
                if (user5.getType() == 2) {
                    if (user5.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue5 = (int) Math.ceil(user5.getEnergyvalue() * tbDividendsl5.getIntegralConsumption());
                        if ((user5.getEnergyvalue() - newvalue5) >= 0) {
                            user5.setEnergyvalue(user5.getEnergyvalue() - newvalue5);
                            if (user5.getCharmvalue() > tbDividendsl5.getIntegralMax()) {
                                if (user5.getEnergyvaluelevel() < 5) {
                                    user5.setEnergyvaluelevel(user5.getEnergyvaluelevel() + 1);
                                }
                            } else {
                                user5.setEnergyvaluelevel(user5.getEnergyvaluelevel() - 1);
                            }
                            tbUserService.update(user5);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue5);
                            tbEnergyvalueNotes.setUid(user5.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                } else if (user5.getType() == 1) {
                    if (user5.getEnergyvalue() >= 1) {
                        //小数处理(有值就递进一位)
                        Integer newvalue5 = (int) Math.ceil(user5.getEnergyvalue() * tbDividendsl5.getUserintegralconsumption());
                        if ((user5.getEnergyvalue() - newvalue5) >= 0) {
                            user5.setEnergyvalue(user5.getEnergyvalue() - newvalue5);
                            if (user5.getCharmvalue() > tbDividendsl5.getIntegralMax()) {
                                if (user5.getEnergyvaluelevel() < 5) {
                                    user5.setEnergyvaluelevel(user5.getEnergyvaluelevel() + 1);
                                }
                            } else {
                                user5.setEnergyvaluelevel(user5.getEnergyvaluelevel() - 1);
                            }
                            tbUserService.update(user5);
                            //代理积分消耗明细
                            TbEnergyvalueNotes tbEnergyvalueNotes = new TbEnergyvalueNotes();
                            tbEnergyvalueNotes.setCreattime(new Date());
                            tbEnergyvalueNotes.setType(1);
                            tbEnergyvalueNotes.setOrderid(0);
                            tbEnergyvalueNotes.setValue(newvalue5);
                            tbEnergyvalueNotes.setUid(user5.getUuid());
                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes);
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock1.unlock();
        }

    }


    /**
     * 定时任务 团购活动监听
     *
     * @throws Exception
     */
    @Scheduled(fixedRate = 30000)//每30秒执行一次
    public void play() throws Exception {
        ReentrantLock lock2 = new ReentrantLock();
        /*
        团购
         */
        try {
            lock2.lock();
            Date date = new Date();
            List<TbGroupbuy> list = tbGroupbuyService.foudList();
            for (int i = 0; i < list.size(); i++) {
                TbGroupbuy tbGroupbuy = list.get(i);
                if (tbGroupbuy.getStarttime().getTime() <= date.getTime()) {
                    tbGroupbuy.setIsopen(1);
                    tbGroupbuyService.update(tbGroupbuy);
                }
                if (tbGroupbuy.getEndtime().getTime() <= date.getTime()) {
                    tbGroupbuy.setIsopen(2);
                    tbGroupbuyService.update(tbGroupbuy);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock2.unlock();
        }
    }


    /**
     * 定时任务 活动监听
     */
    @Scheduled(fixedRate = 60000)//每60秒执行一次
    public void play3() {
        ReentrantLock lock = new ReentrantLock();
         /*
        活动
         */
        try {
            lock.lock();
            Date date = new Date();
            List<TbActivity> list = tbActivityServices.findNoClosed();
            for (TbActivity tbactivity : list) {
                if (tbactivity.getStarttime().getTime() <= date.getTime() && tbactivity.getEndtime().getTime() > date.getTime()) {
                    tbactivity.setIsopen(1);
                    tbActivityServices.updateByPrimaryKey(tbactivity);
                }
                if (tbactivity.getEndtime().getTime() <= date.getTime()) {
                    tbactivity.setIsopen(2);
                    tbActivityServices.updateByPrimaryKey(tbactivity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 定时任务 团购到时间监听
     *
     * @throws Exception
     */
    @Scheduled(fixedRate = 120000)//每120秒执行一次
    public void play4() {
        Date date = new Date();
        //获取所有团购信息
        List<TbGroupbuyUser> list = tbGroupbuyUserService.queryByAllLeader();
        for (TbGroupbuyUser tgbu : list) {
            //判断团购时间是否到时
            if (tgbu.getEndtime().getTime() <= date.getTime()) {
                //该团购到时间，改结束生成团购订单
                //获取该团购参与的人
                Map<String, Object> map = new HashedMap();
                map.put("leaderid", tgbu.getUserid());
                map.put("groupid", tgbu.getGroupbuyid());
                List<TbGroupbuyUser> groupbuyUserList = tbGroupbuyUserService.queryByTeam(map);
                //生成订单
                for (TbGroupbuyUser tgu : groupbuyUserList) {
                    //团长订单
                    if (tgu.getLeaderid().equals(tgu.getUserid())) {
                        TbUser tbUser = tbUserService.queryById(tgu.getUserid());
                        if (tbUser==null){
                            continue;
                        }
                        //查询商品信息
                        TbGroupbuy tbGroupbuy = tbGroupbuyService.queryObject(tgu.getGroupbuyid());
                        TbCommodity tbCommodity = tbCommodityService.queryObject(tbGroupbuy.getCommodityid());
                        //创建购物车数据
                        TbShoppingCart tbShoppingCart = new TbShoppingCart();
                        tbShoppingCart.setStatus(true);
                        tbShoppingCart.setCommoditynumber(1);
                        tbShoppingCart.setPricediscounts(tbCommodity.getPricediscounts());
                        tbShoppingCart.setPrice(tgu.getPrice());
                        tbShoppingCart.setFreight(0f);
                        tbShoppingCart.setCommodityname(tbCommodity.getCommodityname());
                        tbShoppingCart.setCommoditypic(tbCommodity.getPicurl());
                        tbShoppingCart.setShoppingtime(new Date());
                        tbShoppingCart.setSelected(null);
                        tbShoppingCart.setUid(tbUser.getUuid());
                        tbShoppingCart.setCommodityid(tbCommodity.getId());
                        tbShoppingCartServices.insert(tbShoppingCart);
                        tgu.setIsend(1);
                        tbGroupbuyUserService.updateByPrimaryKey(tgu);
                        TbOrder tbOrder = new TbOrder();
                        tbOrder.setUid(tbUser.getUuid());
                        tbOrder.setChannel("1");
                        tbOrder.setIsdelete(false);
                        tbOrder.setShoppingcartid("[" + String.valueOf(tbShoppingCart.getId()) + "]");
                        tbOrder.setIsfirst(1);
                        tbOrder.setUsername(tbUser.getUsername());
                        tbOrder.setTotalfee(tgu.getPrice());
                        tbOrder.setPaytype(PayStatus.unpaid.getName());
                        tbOrder.setBillno(OrderUtil.getOrderNo());
                        tbOrder.setGuid(UidUtil.getUUID());
                        tbOrder.setIspay(false);
                        tbOrder.setCommodityid(tbGroupbuy.getCommodityid());
                        tbOrder.setRemark("团长");
                        tbOrder.setAddressid(null);
                        tbOrder.setCreatedate(new Date());
                        tbOrder.setOrderstatus(PayStatus.unpaid.getName());
                        tbOrder.setIsproxy(0);
                        List<TbAddress> tbAddressList = tbAddressService.findbyUid(tbUser.getUuid());
                        for (int k = 0; k < tbAddressList.size(); k++) {
                            if (tbAddressList.size() > 0) {
                                if (tbAddressList.get(k).getIsdefault()) {
                                    tbOrder.setAddressid(tbAddressList.get(k).getId());
                                    break;
                                } else {
                                    tbOrder.setAddressid(tbAddressList.get(k).getId());
                                }
                            }
                        }
                        tbOrderService.save(tbOrder);
                    } else {
                        TbUser tbUser = tbUserService.queryById(tgu.getUserid());
                        //查询商品信息
                        TbGroupbuy tbGroupbuy = tbGroupbuyService.queryObject(tgu.getGroupbuyid());
                        TbCommodity tbCommodity = tbCommodityService.queryObject(tbGroupbuy.getCommodityid());
                        //创建购物车数据
                        TbShoppingCart tbShoppingCart = new TbShoppingCart();
                        tbShoppingCart.setStatus(true);
                        tbShoppingCart.setCommoditynumber(1);
                        tbShoppingCart.setPricediscounts(tbCommodity.getPricediscounts());
                        tbShoppingCart.setPrice(tgu.getPrice());
                        tbShoppingCart.setFreight(0f);
                        tbShoppingCart.setCommodityname(tbCommodity.getCommodityname());
                        tbShoppingCart.setCommoditypic(tbCommodity.getPicurl());
                        tbShoppingCart.setShoppingtime(new Date());
                        tbShoppingCart.setSelected(null);
                        tbShoppingCart.setUid(tbUser.getUuid());
                        tbShoppingCart.setCommodityid(tbCommodity.getId());
                        tbShoppingCartServices.insert(tbShoppingCart);
                        tgu.setIsend(1);
                        tbGroupbuyUserService.updateByPrimaryKey(tgu);
                        TbOrder tbOrder = new TbOrder();
                        tbOrder.setUid(tbUser.getUuid());
                        tbOrder.setChannel("1");
                        tbOrder.setIsdelete(false);
                        tbOrder.setShoppingcartid("[" + String.valueOf(tbShoppingCart.getId()) + "]");
                        tbOrder.setIsfirst(1);
                        tbOrder.setUsername(tbUser.getUsername());
                        tbOrder.setTotalfee(tgu.getPrice());
                        tbOrder.setPaytype(PayStatus.unpaid.getName());
                        tbOrder.setBillno(OrderUtil.getOrderNo());
                        tbOrder.setGuid(UidUtil.getUUID());
                        tbOrder.setIspay(false);
                        tbOrder.setCommodityid(tbGroupbuy.getCommodityid());
                        tbOrder.setRemark("团员");
                        tbOrder.setAddressid(null);
                        tbOrder.setCreatedate(new Date());
                        tbOrder.setOrderstatus(PayStatus.unpaid.getName());
                        tbOrder.setIsproxy(0);
                        List<TbAddress> tbAddressList = tbAddressService.findbyUid(tbUser.getUuid());
                        for (int k = 0; k < tbAddressList.size(); k++) {
                            if (tbAddressList.size() > 0) {
                                if (tbAddressList.get(k).getIsdefault()) {
                                    tbOrder.setAddressid(tbAddressList.get(k).getId());
                                    break;
                                } else {
                                    tbOrder.setAddressid(tbAddressList.get(k).getId());
                                }
                            }
                        }
                        tbOrderService.save(tbOrder);
                    }
                }
            }
        }
    }


    /**
     * 全球分红 (每天) 金额：分
     *
     * @throws Exception
     */
    @Scheduled(cron = "0 0 2 * * ?") // 每天凌晨2点执行
    public void doSomething() throws Exception {
        ReentrantLock lock = new ReentrantLock();
        Calendar calendar = Calendar.getInstance();
        try {
            lock.lock();
            //获取每天全球分红资金池金额
            TbCapitalpool tbCapitalpool = tbCapitalpoolService.queryById(1);
            Float money = tbCapitalpool.getAmount();

            //获取魅力值等级为1的用户
            List<TbUser> userList1 = tbUserService.queryByintegrallevel1(1);
            //获取魅力值等级为1的分红数据
            TbDividends tbDividends1 = tbDividendsService.queryByLevel(1);
            //计算魅力值等级为1的的分红金额
            Float sumMoney1 = money * tbDividends1.getDialRatio();
            //获取均价
            Float avgMoney1 = sumMoney1 / userList1.size();
            //第一级
            for (TbUser user : userList1) {
                user.setMemberdividend((int) (user.getMemberdividend() + avgMoney1));
                user.setBalance((int) (user.getBalance() + avgMoney1));
                tbUserService.update(user);
                //全球分红明细
                TbRaidDividendDetails tbRaidDividendDetails = new TbRaidDividendDetails();
                tbRaidDividendDetails.setAmount((int) (avgMoney1 + 0));
                tbRaidDividendDetails.setCreatetime(new Date());
                tbRaidDividendDetails.setBenefitUserid(user.getUserId());
                tbRaidDividendDetails.setBenefitUsername(user.getUsername());
                tbRaidDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                tbRaidDividendDetailsService.insert(tbRaidDividendDetails);

            }

            //获取魅力值等级为2的用户
            List<TbUser> userList2 = tbUserService.queryByintegrallevel1(2);
            //获取魅力值等级为2的分红数据
            TbDividends tbDividends2 = tbDividendsService.queryByLevel(2);
            //计算魅力值等级为2的的分红金额
            Float sumMoney2 = money * tbDividends2.getDialRatio();
            //获取均价
            Float avgMoney2 = sumMoney2 / userList2.size();
            //第二级
            for (TbUser user : userList2) {
                user.setMemberdividend((int) (user.getMemberdividend() + avgMoney2));
                user.setBalance((int) (user.getBalance() + avgMoney2));
                tbUserService.update(user);
                //全球分红明细
                TbRaidDividendDetails tbRaidDividendDetails = new TbRaidDividendDetails();
                tbRaidDividendDetails.setAmount((int) (avgMoney2 + 0));
                tbRaidDividendDetails.setCreatetime(new Date());
                tbRaidDividendDetails.setBenefitUserid(user.getUserId());
                tbRaidDividendDetails.setBenefitUsername(user.getUsername());
                tbRaidDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                tbRaidDividendDetailsService.insert(tbRaidDividendDetails);
            }

            //获取魅力值等级为3的用户
            List<TbUser> userList3 = tbUserService.queryByintegrallevel1(3);
            //获取魅力值等级为3的分红数据
            TbDividends tbDividends3 = tbDividendsService.queryByLevel(3);
            //计算魅力值等级为3的的分红金额
            Float sumMoney3 = money * tbDividends3.getDialRatio();
            //获取均价
            Float avgMoney3 = sumMoney3 / userList3.size();
            //第三级
            for (TbUser user : userList3) {
                user.setMemberdividend((int) (user.getMemberdividend() + avgMoney3));
                user.setBalance((int) (user.getBalance() + avgMoney3));
                tbUserService.update(user);
                //全球分红明细
                TbRaidDividendDetails tbRaidDividendDetails = new TbRaidDividendDetails();
                tbRaidDividendDetails.setAmount((int) (avgMoney3 + 0));
                tbRaidDividendDetails.setCreatetime(new Date());
                tbRaidDividendDetails.setBenefitUserid(user.getUserId());
                tbRaidDividendDetails.setBenefitUsername(user.getUsername());
                tbRaidDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                tbRaidDividendDetailsService.insert(tbRaidDividendDetails);
            }

            //获取魅力值等级为4的用户
            List<TbUser> userList4 = tbUserService.queryByintegrallevel1(4);
            //获取魅力值等级为4的分红数据
            TbDividends tbDividends4 = tbDividendsService.queryByLevel(4);
            //计算魅力值等级为4的的分红金额
            Float sumMoney4 = money * tbDividends4.getDialRatio();
            //获取均价
            Float avgMoney4 = sumMoney4 / userList4.size();
            //第四级
            for (TbUser user : userList4) {
                user.setMemberdividend((int) (user.getMemberdividend() + avgMoney4));
                user.setBalance((int) (user.getBalance() + avgMoney4));
                tbUserService.update(user);
                //全球分红明细
                TbRaidDividendDetails tbRaidDividendDetails = new TbRaidDividendDetails();
                tbRaidDividendDetails.setAmount((int) (avgMoney4 + 0));
                tbRaidDividendDetails.setCreatetime(new Date());
                tbRaidDividendDetails.setBenefitUserid(user.getUserId());
                tbRaidDividendDetails.setBenefitUsername(user.getUsername());
                tbRaidDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                tbRaidDividendDetailsService.insert(tbRaidDividendDetails);
            }

            //获取魅力值等级为5的用户
            List<TbUser> userList5 = tbUserService.queryByintegrallevel1(5);
            //获取魅力值等级为5的分红数据
            TbDividends tbDividends5 = tbDividendsService.queryByLevel(5);
            //计算魅力值等级为5的的分红金额
            Float sumMoney5 = money * tbDividends5.getDialRatio();
            //获取均价
            Float avgMoney5 = sumMoney5 / userList5.size();
            //第五级
            for (TbUser user : userList5) {
                user.setMemberdividend((int) (user.getMemberdividend() + avgMoney5));
                user.setBalance((int) (user.getBalance() + avgMoney5));
                tbUserService.update(user);
                //全球分红明细
                TbRaidDividendDetails tbRaidDividendDetails = new TbRaidDividendDetails();
                tbRaidDividendDetails.setAmount((int) (avgMoney5 + 0));
                tbRaidDividendDetails.setCreatetime(new Date());
                tbRaidDividendDetails.setBenefitUserid(user.getUserId());
                tbRaidDividendDetails.setBenefitUsername(user.getUsername());
                tbRaidDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                tbRaidDividendDetailsService.insert(tbRaidDividendDetails);
            }

            //获取魅力值等级为6的用户
            List<TbUser> userList6 = tbUserService.queryByintegrallevel1(6);
            //获取魅力值等级为6的分红数据
            TbDividends tbDividends6 = tbDividendsService.queryByLevel(6);
            //计算魅力值等级为6的的分红金额
            Float sumMoney6 = money * tbDividends6.getDialRatio();
            //获取均价
            Float avgMoney6 = sumMoney6 / userList6.size();
            //第六级
            for (TbUser user : userList6) {
                user.setMemberdividend((int) (user.getMemberdividend() + avgMoney6));
                user.setBalance((int) (user.getBalance() + avgMoney6));
                tbUserService.update(user);
                //全球分红明细
                TbRaidDividendDetails tbRaidDividendDetails = new TbRaidDividendDetails();
                tbRaidDividendDetails.setAmount((int) (avgMoney6 + 0));
                tbRaidDividendDetails.setCreatetime(new Date());
                tbRaidDividendDetails.setBenefitUserid(user.getUserId());
                tbRaidDividendDetails.setBenefitUsername(user.getUsername());
                tbRaidDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                tbRaidDividendDetailsService.insert(tbRaidDividendDetails);
            }

            //获取魅力值等级为7的用户
            List<TbUser> userList7 = tbUserService.queryByintegrallevel1(7);
            //获取魅力值等级为7的分红数据
            TbDividends tbDividends7 = tbDividendsService.queryByLevel(7);
            //计算魅力值等级为7的的分红金额
            Float sumMoney7 = money * tbDividends6.getDialRatio();
            //获取均价
            Float avgMoney7 = sumMoney7 / userList7.size();
            //第七级
            for (TbUser user : userList7) {
                user.setMemberdividend((int) (user.getMemberdividend() + avgMoney7));
                user.setBalance((int) (user.getBalance() + avgMoney7));
                tbUserService.update(user);
                //全球分红明细
                TbRaidDividendDetails tbRaidDividendDetails = new TbRaidDividendDetails();
                tbRaidDividendDetails.setAmount((int) (avgMoney7 + 0));
                tbRaidDividendDetails.setCreatetime(new Date());
                tbRaidDividendDetails.setBenefitUserid(user.getUserId());
                tbRaidDividendDetails.setBenefitUsername(user.getUsername());
                tbRaidDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                tbRaidDividendDetailsService.insert(tbRaidDividendDetails);
            }
            //计算分红后的金额
            Float money2 = money - sumMoney1 - sumMoney2 - sumMoney3 - sumMoney4 - sumMoney5 - sumMoney6 - sumMoney7;
            tbCapitalpool.setAmount(money2);
            tbCapitalpoolService.update(tbCapitalpool);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    /**
     * 代理分红 (每周)
     *
     * @throws Exception
     */
//    @Scheduled(fixedRate = 5000)
    @Scheduled(cron = "0 0 4 ? * MON") // 每周一4：00分执行任务
    public void goWork() throws Exception {
        ReentrantLock lock = new ReentrantLock();
        try {
            lock.lock();
            Calendar calendar = Calendar.getInstance();
            //获取每周代理分红资金池金额
            TbCapitalpool tbCapitalpool = tbCapitalpoolService.queryById(2);
            Float money = tbCapitalpool.getAmount();


            //获取能量值等级为1的用户 energyvaluelevel
            List<TbUser> userList1 = tbUserService.queryenergyvaluelevel(1);
            //获取能力值等级为1的分红数据
            TbDividends tbDividends1 = tbDividendsService.queryByLevel2(1);
            //计算能力值等级为1的的分红金额
            Float sumMoney1 = money * tbDividends1.getDialRatio();
            //获取均价
            Float avgMoney1 = sumMoney1 / userList1.size();
            //第1级
            List<TbUser> userList11 = new ArrayList<>();
            for (TbUser user : userList1) {
                if (user.getType() == 2) {
                    userList11.add(user);
                }
            }
            for (TbUser user : userList11) {
                if (user.getType() == 2) {
                    user.setProxydividend((int) (user.getProxydividend() + avgMoney1));
                    user.setBalance((int) (user.getBalance() + avgMoney1));
                    tbUserService.update(user);
                    String money1str = avgMoney1 / 100d + "元";
                    WeiXinMsgUtil.dailifenhong(user.getWxopenid(), money1str);
                    //代理分红明细
                    TbProxyDividendDetails tbProxyDividendDetails = new TbProxyDividendDetails();
                    tbProxyDividendDetails.setAmount((int) (avgMoney1 + 0));
                    tbProxyDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                    tbProxyDividendDetails.setCreatetime(new Date());
                    tbProxyDividendDetails.setBenefitUserid(user.getUserId());
                    tbProxyDividendDetails.setBenefitUsername(user.getUsername());
                    tbProxyDividendDetailsService.insert(tbProxyDividendDetails);
                }
            }
            //获取能量值等级为2的用户
            List<TbUser> userList2 = tbUserService.queryenergyvaluelevel(2);
            //获取能力值等级为2的分红数据
            TbDividends tbDividends2 = tbDividendsService.queryByLevel2(2);
            //计算能力值等级为2的的分红金额
            Float sumMoney2 = money * tbDividends2.getDialRatio();
            //获取均价
            Float avgMoney2 = sumMoney2 / userList2.size();
            //第2级
            List<TbUser> userList22 = new ArrayList<>();
            for (TbUser user : userList2) {
                if (user.getType() == 2) {
                    userList22.add(user);
                }
            }
            for (TbUser user : userList22) {
                if (user.getType() == 2) {
                    user.setProxydividend((int) (user.getProxydividend() + avgMoney2));
                    user.setBalance((int) (user.getBalance() + avgMoney2));
                    tbUserService.update(user);
                    String money2str = avgMoney2 / 100d + "元";
                    WeiXinMsgUtil.dailifenhong(user.getWxopenid(), money2str);
                    //代理分红明细
                    TbProxyDividendDetails tbProxyDividendDetails = new TbProxyDividendDetails();
                    tbProxyDividendDetails.setAmount((int) (avgMoney2 + 0));
                    tbProxyDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                    tbProxyDividendDetails.setCreatetime(new Date());
                    tbProxyDividendDetails.setBenefitUserid(user.getUserId());
                    tbProxyDividendDetails.setBenefitUsername(user.getUsername());
                    tbProxyDividendDetailsService.insert(tbProxyDividendDetails);
                }
            }
            //获取能量值等级为3的用户
            List<TbUser> userList3 = tbUserService.queryenergyvaluelevel(3);
            //获取能力值等级为3的分红数据
            TbDividends tbDividends3 = tbDividendsService.queryByLevel2(3);
            //计算能力值等级为3的的分红金额
            Float sumMoney3 = money * tbDividends3.getDialRatio();
            //获取均价
            Float avgMoney3 = sumMoney3 / userList3.size();
            //第3级
            List<TbUser> userList33 = new ArrayList<>();
            for (TbUser user : userList3) {
                if (user.getType() == 2) {
                    userList33.add(user);
                }
            }
            for (TbUser user : userList33) {
                if (user.getType() == 2) {
                    user.setProxydividend((int) (user.getProxydividend() + avgMoney3));
                    user.setBalance((int) (user.getBalance() + avgMoney3));
                    tbUserService.update(user);
                    String money3str = avgMoney3 / 100d + "元";
                    WeiXinMsgUtil.dailifenhong(user.getWxopenid(), money3str);
                    //代理分红明细
                    TbProxyDividendDetails tbProxyDividendDetails = new TbProxyDividendDetails();
                    tbProxyDividendDetails.setAmount((int) (avgMoney3 + 0));
                    tbProxyDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                    tbProxyDividendDetails.setCreatetime(new Date());
                    tbProxyDividendDetails.setBenefitUserid(user.getUserId());
                    tbProxyDividendDetails.setBenefitUsername(user.getUsername());
                    tbProxyDividendDetailsService.insert(tbProxyDividendDetails);
                }
            }
            //获取能量值等级为4的用户
            List<TbUser> userList4 = tbUserService.queryenergyvaluelevel(4);
            //获取能力值等级为4的分红数据
            TbDividends tbDividends4 = tbDividendsService.queryByLevel2(4);
            //计算能力值等级为4的的分红金额
            Float sumMoney4 = money * tbDividends4.getDialRatio();
            //获取均价
            Float avgMoney4 = sumMoney4 / userList4.size();
            //第4级
            List<TbUser> userList44 = new ArrayList<>();
            for (TbUser user : userList4) {
                if (user.getType() == 2) {
                    userList44.add(user);
                }
            }
            for (TbUser user : userList44) {
                if (user.getType() == 2) {
                    user.setProxydividend((int) (user.getProxydividend() + avgMoney4));
                    user.setBalance((int) (user.getBalance() + avgMoney4));
                    tbUserService.update(user);
                    //代理分红明细
                    TbProxyDividendDetails tbProxyDividendDetails = new TbProxyDividendDetails();
                    tbProxyDividendDetails.setAmount((int) (avgMoney4 + 0));
                    tbProxyDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                    tbProxyDividendDetails.setCreatetime(new Date());
                    tbProxyDividendDetails.setBenefitUserid(user.getUserId());
                    tbProxyDividendDetails.setBenefitUsername(user.getUsername());
                    tbProxyDividendDetailsService.insert(tbProxyDividendDetails);
                }
            }
            //获取能量值等级为5的用户 energyvaluelevel
            List<TbUser> userList5 = tbUserService.queryenergyvaluelevel(5);
            //获取能力值等级为5的分红数据
            TbDividends tbDividends5 = tbDividendsService.queryByLevel2(5);
            //计算能力值等级为5的的分红金额
            Float sumMoney5 = money * tbDividends5.getDialRatio();
            //获取均价
            Float avgMoney5 = sumMoney5 / userList5.size();
            //第5级
            List<TbUser> userList55 = new ArrayList<>();
            for (TbUser user : userList5) {
                if (user.getType() == 2) {
                    userList55.add(user);
                }
            }
            for (TbUser user : userList55) {
                if (user.getType() == 2) {
                    user.setProxydividend((int) (user.getProxydividend() + avgMoney5));
                    user.setBalance((int) (user.getBalance() + avgMoney5));
                    tbUserService.update(user);
                    //代理分红明细
                    TbProxyDividendDetails tbProxyDividendDetails = new TbProxyDividendDetails();
                    tbProxyDividendDetails.setAmount((int) (avgMoney5 + 0));
                    tbProxyDividendDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
                    tbProxyDividendDetails.setCreatetime(new Date());
                    tbProxyDividendDetails.setBenefitUserid(user.getUserId());
                    tbProxyDividendDetails.setBenefitUsername(user.getUsername());
                    tbProxyDividendDetailsService.insert(tbProxyDividendDetails);
                }
            }
            //计算分红后的金额
            Float money2 = money - sumMoney1 - sumMoney2 - sumMoney3 - sumMoney4 - sumMoney5;
            tbCapitalpool.setAmount(money2);
            tbCapitalpoolService.update(tbCapitalpool);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }


    /**
     * 回复用户提现次数
     */
    @Async
    @Scheduled(cron = "0 0 5 1 * ?") //每月1号凌晨1点执行一次
    public void tixiancishu() {
        TbDataInfo tbDataInfo = sysDataInfoService.queryObject(1);
        //获取所有用户
        List<TbUser> userList = tbUserService.queryAll();
        for (TbUser user : userList) {
            user.setWithdrawaltimes(tbDataInfo.getDate());
            tbUserService.update(user);
        }
    }


    /**
     * 判断砍价生成订单
     *
     * @throws Exception
     */
    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public void kanjia() throws Exception {
        ReentrantLock lock = new ReentrantLock();
        lock.lock();
        try {
            Date oneDay = DateUtils.preDay(1);
            //获取所有发起的砍价信息,1天以内的数据
            List<TbBargain> tbBargainList = tbBargainService.findNoClose(oneDay);
            for (int i = 0; i < tbBargainList.size(); i++) {
                TbBargain tbBargain = tbBargainList.get(i);
                if (tbBargain.getIsend()) continue;

                Map<String, Object> params1 = new HashMap<>();
                params1.put("userid", tbBargain.getUserid());
                params1.put("commodityid", tbBargain.getCommodityid());
                params1.put("activityid", tbBargain.getActivityid());
                params1.put("page", 1);
                params1.put("limit", Integer.MAX_VALUE);
                Query query2 = new Query(params1);
                Page<TbBargain> pageUtil2 = new Page<TbBargain>(query2.getPage(), query2.getLimit());
                Page<TbBargain> page2 = tbBargainService.queryPageList(pageUtil2, query2);

                //未结束才去判断是否创建订单 已结束表示已经生成过订单！
                TbUser tbUser = tbUserService.queryById(tbBargain.getUserid());

                int money = 0;//已砍价格
                //砍价结束的时间
                long endTime = DateUtils.calculateMin(tbBargain.getCreatedate(), 1, ChronoUnit.HOURS).getTime();

                if (page2.getRecords().size() < 10 && endTime > new Date().getTime())
                    continue;
                for (int k = 0; k < page2.getRecords().size(); k++) {
                    page2.getRecords().get(k).setIsend(true);
                    money += page2.getRecords().get(k).getPrice();
                    tbBargainService.updateByPrimaryKey(page2.getRecords().get(k));
                }

                //查询商品数据
                TbCommodity tbCommodity = tbCommodityService.queryObject(page2.getRecords().get(i).getCommodityid());

                //创建购物车数据
                TbShoppingCart tbShoppingCart = new TbShoppingCart();
                tbShoppingCart.setStatus(true);
                tbShoppingCart.setCommoditynumber(1);
                tbShoppingCart.setPricediscounts(tbCommodity.getPricediscounts());
                tbShoppingCart.setPrice(tbCommodity.getPrice());
                tbShoppingCart.setFreight(0f);
                tbShoppingCart.setCommodityname(tbCommodity.getCommodityname());
                tbShoppingCart.setCommoditypic(tbCommodity.getPicurl());
                tbShoppingCart.setShoppingtime(new Date());
                tbShoppingCart.setSelected(null);
                tbShoppingCart.setUid(tbUser.getUuid());
                tbShoppingCart.setCommodityid(tbCommodity.getId());
                tbShoppingCartServices.insert(tbShoppingCart);

                //创建订单
                TbOrder tbOrder = new TbOrder();
                tbOrder.setIsdelete(false);
                tbOrder.setCreatedate(new Date());
                tbOrder.setAddressid(null);
                tbOrder.setBillno(OrderUtil.getOrderNo());
                tbOrder.setGuid(UidUtil.getUUID());
                tbOrder.setUid(tbUser.getUuid());
                tbOrder.setIspay(false);
                tbOrder.setOrderstatus(PayStatus.unpaid.getName());
                tbOrder.setUsername(tbUser.getUsername());
                tbOrder.setShoppingcartid(null);
                tbOrder.setChannel("2");
                List<TbAddress> tbAddressList = tbAddressService.findbyUid(tbUser.getUuid());
                for (int k = 0; k < tbAddressList.size(); k++) {
                    if (tbAddressList.size() > 0) {
                        if (tbAddressList.get(k).getIsdefault()) {
                            tbOrder.setAddressid(tbAddressList.get(k).getId());
                            break;
                        } else {
                            tbOrder.setAddressid(tbAddressList.get(k).getId());
                        }
                    }
                }
                TbActivityCommodity tbActivityCommodity = tbActivityCommodityDao.foundByCAndA(tbBargain.getCommodityid(), tbBargain.getActivityid());
                tbOrder.setCommodityid(tbBargain.getCommodityid());
                tbOrder.setRemark("砍价");
                tbOrder.setAid(tbBargain.getActivityid());
                tbOrder.setTotalfee(tbActivityCommodity.getNowprice()-money);
                tbOrder.setShoppingcartid("[" + String.valueOf(tbShoppingCart.getId()) + "]");
                List<TbOrder> list = tbOrderService.queryByUid(tbOrder.getUid());
                //判断是否是首单
                if (list.size() <= 0) {
                    tbOrder.setIsfirst(0);
                } else {
                    tbOrder.setIsfirst(1);
                }
                //判断是否为代理
                TbUser tbUser1 = tbUserService.queryById(tbUser.getParentid());
                if (tbUser1 != null) {
                    if (tbUser1.getType() == 2) {
                        tbOrder.setProxyuserid(tbUser1.getUserId());
                        tbOrder.setIsproxy(1);
                    } else {
                        tbOrder.setIsproxy(0);
                    }
                }
                //创建订单
                tbOrderService.save(tbOrder);
                String moneystr = tbOrder.getTotalfee() / 100d + "元";
                WeiXinMsgUtil.kanjiaMsg(tbCommodity.getCommodityname(), moneystr, tbUser.getWxopenid());
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    @Autowired
    private SysTbActivityCommodityService sysTbActivityCommodityService;

    /**
     * 过期时间
     */
    public static final int EXPIRT_HOUR = 24;

    /**
     * 活动订单过期
     * 1.订单过期不能支付
     * 2.占用的活动库存恢复
     */
    @Scheduled(initialDelay = 1000*60*3,fixedDelay = 1000*60)
    public void activeOrderExpire() {
        //查询最近三天的,参与活动的订单
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime time = now.minus(3, ChronoUnit.DAYS);
        Date startTime = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
        //查询这个时间之后的订单,且参与活动,未支付
        List<TbOrder> tbOrders = tbOrderService.findActiveOrderByTime(startTime);


        LocalDateTime expir = now.minus(EXPIRT_HOUR, ChronoUnit.HOURS);
        Date expirTime = Date.from(expir.atZone(ZoneId.systemDefault()).toInstant());
        for (TbOrder order : tbOrders) {
            if (order.getCreatedate().after(expirTime)) {
                //未到过期时间的订单,不用处理
                continue;
            }
            JSONArray ja = JSONUtils.toJSONArray(order.getShoppingcartid());
            if (ja.size() > 1 || ja.size() == 0) {//参与活动的商品详细应该有且只有一个
                continue;
            } else {
                TbShoppingCart tbShoppingCart = tbShoppingCartServices.selectByPrimaryKey(ja.getInt(0));
                //减少销量
                sysTbActivityCommodityService.subStore(order.getAid(), tbShoppingCart.getCommoditynumber());
            }
            //删除订单
            order.setIsdelete(true);
            tbOrderService.update(order);
        }
    }

}
